<?php

namespace app\app\controller;

use app\app\model\PreliminaryDocumentsModel;
use app\app\model\ProjectModel;
use app\app\model\ProphaseModel;
use app\app\model\RandModel;
use app\app\model\SysDictDataModel;
use app\BaseController;
use think\db\exception\DbException;
use think\facade\Db;

class PreliminaryDocuments extends BaseController
{
    /**
     * 前期文件列表
     * @return void
     * @throws DbException
     */
    public function list()
    {
        $project_id = input('project_id', '');
        if (empty($project_id)) $this->error('请传入project_id');

        $type = input('type', '');
        empty($type) && $this->error('请传入type');


        $project_info = ProjectModel::where('id', $project_id)->find();
        if ($type == '前期资料文件') {
            $list = ProphaseModel::alias('a')->field('a.id,"前期资料文件" as type,a.file_date as done_time,a.file_no,b.file_name,b.file_path,c.dict_label as name')
                ->leftjoin('sys_file_info b', 'b.file_id = a.file_id')
                ->leftjoin('sys_dict_data c', 'c.dict_value = a.file_type')
                ->where('a.project_id', $project_info['link_project_id'])
                ->where('c.dict_type', 'zcxwj_type')
                ->select();
        } else {
            $list = Db::table('t_preliminary_documents')
                ->alias('a')
                ->leftJoin('sys_file_info b', 'a.file_id = b.file_id')
                ->field('a.*,b.file_name,b.file_path')
                ->where('project_id', $project_id)
                ->where('type', $type)
                ->select();
        }


        if (empty($list)) $this->error('查询为空');

        $this->success('success', $list);
    }

    /**
     * 前期文件修改
     * @return void
     */
    public function save()
    {
        $id = input('id', '');
        $param = $this->request->param();

        if (empty($id)) {
            $model = new PreliminaryDocumentsModel();
            $model->id = RandModel::randId();
        } else {
            $model = PreliminaryDocumentsModel::where('id', $id);
        }

        $model->save($param);

        $this->success('success');
    }


    /**
     * 获取字典数据
     * @return array|string[]
     */
    public function nameList()
    {
        $type = input('type', '');
        empty($type) && $this->error('请传入类型');

        $data = [];
        switch ($type) {
            case '前期资料文件':
//                $data = [
//                    '规划许可',
//                    '土地预审',
//                    '环境评价',
//                    '地质灾害预估',
//                    '压覆矿产预估',
//                    '水土保持、社会稳定风险、节能预估',
//                    '选址选线报告',
//                ];
                $data = SysDictDataModel::field('dict_code,dict_label')->where('dict_type', 'xzxk_type')->select();

            case '行政许可手续':
//                $data = [
//                    '监理规划',
//                    '项目管理实施规划（施工组织设计）',
//                    '项目进度计划',
//                    '施工安全管控措施等报审表',
//                    '环保监理规划及实施细则',
//                    '水保监理规划及实施细则',
//                    '水保监测实施方案',
//                    '绿色设计策划',
//                ];
                $data = SysDictDataModel::field('dict_code,dict_label')->where('dict_type', 'xzxk_type')->select();
        }

        $this->success('success', $data);
    }


    /**
     * 获取字典数据
     * @return array|string[]
     */
    public function getDicList()
    {
        $type = input('type', '');
        empty($type) && $this->error('请传入类型');

        $data = [];
        switch ($type) {
            case '前期资料文件':
                $data = [
                    '规划许可',
                    '土地预审',
                    '环境评价',
                    '地质灾害预估',
                    '压覆矿产预估',
                    '水土保持、社会稳定风险、节能预估',
                    '选址选线报告',
                ];

            case '行政许可手续':
                $data = [
                    '监理规划',
                    '项目管理实施规划（施工组织设计）',
                    '项目进度计划',
                    '施工安全管控措施等报审表',
                    '环保监理规划及实施细则',
                    '水保监理规划及实施细则',
                    '水保监测实施方案',
                    '绿色设计策划',
                ];
        }

        return $data;
    }
}